package com.bidding.auth.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.bidding.auth.pojo.dto.LoginDTO;
import com.bidding.auth.pojo.vo.AuthCaptchaVO;
import com.bidding.auth.service.AuthService;
import com.bidding.common.model.Result;
import com.google.common.base.Preconditions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

/**
 * 认证控制器
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@Api(tags = "认证管理")
public class AuthController {

    @Resource
    private AuthService authService;

    /**
     * 登录接口
     *
     * @param loginDTO 登录DTO
     * @return 登录结果
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户输入账号密码验证码登录")
    public Result<SaTokenInfo> login(@RequestBody LoginDTO loginDTO) {
        try {
            Preconditions.checkArgument(StringUtils.isNotBlank(loginDTO.getUserName()),"账号不能为空");
            Preconditions.checkArgument(StringUtils.isNotBlank(loginDTO.getPassWord()),"密码不能为空");
            Preconditions.checkArgument(StringUtils.isNotBlank(loginDTO.getCaptcha()),"验证码不能为空");
            log.info("用户输入的登录账号：{}，验证码：{}",loginDTO.getUserName(),loginDTO.getCaptcha());

            SaTokenInfo saTokenInfo = authService.login(loginDTO);
            return Result.success(saTokenInfo);
        } catch (Exception e) {
            log.error("登录失败，{}",e.getMessage());
            return Result.error("登录失败");
        }
    }

    /**
     * 登出接口
     *
     * @return 登出结果
     */
    @PostMapping("/logout")
    @ApiOperation("用户登出")
    public Result logout() {
        StpUtil.logout();
        return Result.success("登出成功");
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 用户信息
     */
    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    public Result getUserInfo() {
        // 检查是否登录
        StpUtil.checkLogin();

        // 获取当前登录用户ID
        Object loginId = StpUtil.getLoginId();

        // 此处应该根据loginId查询数据库获取用户信息
        // 为了演示，我们直接返回模拟数据
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("userId", loginId);
        userInfo.put("username", "admin");
        userInfo.put("realName", "管理员");
        userInfo.put("roles", new String[] { "admin" });
        userInfo.put("permissions", new String[] { "system:user:list", "system:user:add" });

        return Result.success(userInfo);
    }

    /**
     * 获取登录验证码
     *
     * @return 验证码图片
     */
    @GetMapping("/captcha")
    @ApiOperation(value = "1生成验证码", notes = "获取图形验证码（Base64格式）")
    public Result<AuthCaptchaVO> captcha() {
        try {
            log.info("验证码接口");
            AuthCaptchaVO authCaptchaVO = authService.captcha();
            return Result.success(authCaptchaVO);
        } catch (Exception e) {
            log.error("验证码生成失败");
            return Result.error("验证码生成失败");
        }
    }

    /**
     * 获取登录验证码
     *
     * @return 验证码图片
     */
    @GetMapping("/captcha2")
    @ApiOperation(value = "生成验证码", notes = "获取图形验证码（Base64格式）")
    public Result<AuthCaptchaVO> captcha2() {
        try {
            log.info("验证码接口");
            AuthCaptchaVO authCaptchaVO = authService.captcha();
            return Result.success(authCaptchaVO);
        } catch (Exception e) {
            log.error("验证码生成失败");
            return Result.error("验证码生成失败");
        }
    }
}